require_relative "twikit_search.rb"

class TwikitTokenizerUI
  def initialize
    puts "Loading.."
    twikit_search = TwikitSearch.new()
    puts "Loaded.", "\n"
    puts "Special instructions:: Q1=> #{@Q1 = "test_queries/simple_adder_utq.rb"}\n"
    puts "Special instructions:: Q2=> #{@Q2 = "test_queries/my_random_utq.rb"}\n"
    puts "Please enter your unit test query filename::"
    puts "(e.g. test_queries/simple_adder_utq.rb,test_file_1.tst)"
    utq = get_fn
    while(utq.length > 0)do
      puts "querying #{utq}"
      if(matches = twikit_search.query_index( utq ))then
        puts "\n\nHere are the best matches that I found::"
        matches[0...3].each{|match|puts match}
      else warn "There was an error while loading the unit test query that you provided" end
      puts "Enter R to repeat query, enter a filepath to run a new query or leave blank to quit:"
      utq = get_fn(utq)
    end
  puts "-------------------------Goodbye!-------------------------"
  puts "----------------------------------------------------------"
  end
  
  def get_fn(prev_query = "")
    fn = convert_special_directives(gets.chomp)
    if(fn.upcase == "R" && prev_query.length > 0)then fn = prev_query end
    return fn
  end
  
  def convert_special_directives( input_dir )
    if(input_dir.upcase === "Q1")then input_dir = @Q1 end
    if(input_dir.upcase === "Q2")then input_dir = @Q2 end
    return input_dir
  end
  
end

TwikitTokenizerUI.new